/* eslint-disable no-new */
var fs = require('fs');

var content;

content = "import App from './App' \n"+
        "import appcanui from 'appcanui'\n";
var geneMainJs = function(distPath, paths){
    //组装组件  拼凑routes
    var routes = '';
    for(var i=0; i<paths.length; i++){
        (function(i){
            var comp = paths[i].substring(paths[i].lastIndexOf('/')+1);
            content += 'import ' + comp
            + ' from \'@/'+paths[i] +'\'\n';

            routes += "    {\n" +
                "       path: '/" +paths[i]+ "',\n"+
                "       name: '"+ comp +"',\n"+
                "       component: "+ comp + "\n"+
              "    },\n";
        })(i)
    }
    routes = routes.substring(0,routes.lastIndexOf(','))+'\n'
    //option
    content += "let op = { \n"+
    "    el: '#app', \n"+
    "    render: v => v(App), \n"+
    "    created () { \n"+
    "    }, \n"+
    "    config: { \n"+
    "      window: { \n"+
    "        navigationBarTitleText: '首页', \n"+
    "        navigationBarBackgroundColor: '#FAFAFA', \n"+
    "        navigationBarTextStyle: '#3b3b3b', \n"+
    "        showNavbar: true \n"+
    "      }, \n"+
    "      tabbar: { \n"+
    "        showTabbar: true, \n"+
    "        color: '#7A7E83', \n"+
    "        selectedColor: '#0074E8', \n"+
    "        borderStyle: 'black', \n"+
    "        backgroudColor: '#ffffff', \n"+
    "        list: [ \n"+
    "          { \n"+
    "            pagePath: '/hello', \n"+
    "            iconPath: 'home', \n"+
    "            selecedIconPath: 'home', \n"+
    "            text: '首页' \n"+
    "          }, \n"+
    "          { \n"+
    "            pagePath: '/test', \n"+
    "            iconPath: 'ucenter-outline', \n"+
    "            selecedIconPath: 'ucenter-outline', \n"+
    "            text: '个人中心' \n"+
    "          } \n"+
    "        ] \n"+
    "      } \n"+
    "    }, \n"+
    "    routes:[\n" +routes+
    "     ]\n"+
      
    "  } \n";
    
    fs.writeFile(distPath+'/main.js', content,function(err){
        if(err) {
            console.log(err);
        }
        else {
            console.log(distPath +' convert main.js finished ');
        }
    });
}

exports.geneMainJs = geneMainJs